<?php
/*
 * employe/upload-photo.phtml
 */
$this->headTitle("Téléchargement d'une Photo");
$fichier = $this->fichier;
?>

<?php     
    $repUplPhoto = str_replace('public/', '', $this->repPhoto);    
?>

<div class="container-fluid">
    <div class="content-head">
        <h4>Téléchargement d'une Photo</h4>
    </div>
    <div class="content-body">
        <div class="preview">
            <?php if (is_null($fichier)): ?>                
                <?php if ($this->error) : ?>       
                <div class="info"><?php echo $this->error; ?></div>
                <?php else: ?>            
                <?php endif; ?>
                
            <?php else: ?>
                <div class="cv-info">
                    <?php $basePath = str_replace('public', '', $this->basePath()); ?>
                    <img src="<?php echo $this->basePath() . '/'. $repUplPhoto . $this->destination ?>" width="200" height="250">
                </div>
                <div class="row">
                    <span class="cv-warning">Vous devez-valider pour prendre en compte le fichier</span>
                    <button id="cv-btn" class="btn btn-info" style="float: right;" onclick="validate();">Valider</button>
                </div>
            <?php endif; ?>
        </div>
        <form id="cv-form" class="well" action="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'uploadPhoto', 'id' => (int) $this->matricule)) ?>" method="post" enctype="multipart/form-data">
            <input type="file" id="cv-file" name="photo" onchange="submit();" accept="image/jpeg,image/gif,image/png">
            <input type="hidden" name="matricule" value="<?php echo (int) $this->matricule; ?>">
        </form>
    </div>
</div>

<script type="text/javascript">
    function validate() {
<?php if ((int) $this->matricule == 0): ?>
            photo = window.opener.document.getElementById('photo');
            photo = $(photo);
            photo.attr('src', '<?php echo $this->basePath() . '/'. $repUplPhoto . $this->destination ?>');
            dataphoto = window.opener.document.getElementById('data-photo');
            dataphoto = $(dataphoto);
            dataphoto.val('<?php echo $this->destination; ?>');
            dataphotoext = window.opener.document.getElementById('data-photo-ext');
            ext = $(dataphotoext);
            ext.val('<?php echo $this->extension; ?>');
            btn = window.opener.document.getElementById('photo-upload');
            btn = $(btn);
<?php else: ?>
            photo = window.opener.document.getElementById('photo');
            photo = $(photo);
            photo.attr('src', '<?php echo $this->basePath() . '/'.$repUplPhoto . $this->destination ?>');
            // mise à jour de la base de données
            ajax = $.ajax({
                type: 'post',
                url: '<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'saveEmployeStringData')) ?>',
                data: '&pk=<?php echo $this->matricule; ?>&name=photo&value=' + encodeURI('<?php echo $this->destination; ?>')
            });
<?php endif; ?>
        //btn.text('Changer le fichier');
        window.close();
    }
</script>

<style type="text/css">
    .cv-file{
        display: inline-block;
        width: 32px;
        height: 32px;
        background: url('<?php echo $this->basePath() ?>/img/rhx/file-32x32.png') no-repeat;
    }
    .cv-info{
         background: none repeat scroll 0 0 #DEDEDE;
	border: 1px solid #CCCCCC;
	color: #111111;
	display: block;
	margin: 5px auto;
	min-height: 60px;
	width: 99%;
    }
     .cv-info img {
	border: 4px solid #FAFAFA;	
	display: block;
	margin: 0 auto;
    }
    .cv-info span{
        line-height: 32px;
    }
    #cv-form{
        position: absolute;
        bottom: -20px;
        left: 0px;
        width: 100%;
    }
    #cv-form [type=file]{
        color: red;
    }
    .cv-warning{
        font-size: 0.9em;
        color: #6666ff;
        font-style: italic;
        text-indent: 5px;
    }
</style>